package com.mall.shopping.services;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductListByPageConverter;
import com.mall.shopping.converter.ShoppingProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

import static com.mall.shopping.constant.GlobalConstants.RECOMMEND_PANEL_ID;

/**
 * @package: com.mall.shopping.services
 * @Description:
 * @author: 北青
 * @date: 2022/1/20 9:03
 */
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ShoppingProductConverter shoppingProductConverter;

    @Autowired
    ProductListByPageConverter productListByPageConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {

        ProductDetailResponse productDetailResponse = new ProductDetailResponse();

        try {
            //参数检查
            request.requestCheck();
            //获取详细信息
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            //参数转换
            ProductDetailDto productDetailDto = shoppingProductConverter.itemAndDesc2Dto(item, itemDesc);
            List<String> imgResult = productDetailDto.getProductImageSmall();

            if (imgResult == null || imgResult.size() == 0) {
                imgResult = new ArrayList<>();
                imgResult.add(productDetailDto.getProductImageBig());
                productDetailDto.setProductImageSmall(imgResult);
            }

            //封装结果
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            productDetailResponse.setProductDetailDto(productDetailDto);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(productDetailResponse, e);
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getProductListByPage(AllProductRequest request) {

        AllProductResponse allProductResponse = new AllProductResponse();
        AllProductResponseResult allProductResponseResult = new AllProductResponseResult();
        Item item = new Item();

        //取出参数
        Integer size = request.getSize();
        Long cid = request.getCid();
        Integer page = request.getPage();
        Integer priceGt = request.getPriceGt();
        Integer priceLte = request.getPriceLte();
        String sort = request.getSort();

        //综合排序
        String orderCol = "price";
        String orderDir = "asc";
        if (!StringUtils.isNumber(sort)) {
            orderCol = "id";
        } else if (Integer.parseInt(sort) == -1) {//价格从低到高
            orderDir = "desc";
        }

        //为空默认最小
        if (priceGt == null) {
            priceGt = 0;
        }
        // 默认最大
        if (priceLte == null) {
            priceLte = Integer.MAX_VALUE;
        }

        try {
            PageHelper.startPage(page, size);
            //获取分页的所有商品
            List<Item> itemList = itemMapper.selectItemFront(cid, orderCol, orderDir, priceGt, priceLte);
            //获取商品总量
            int total = itemMapper.selectCount(item);
            //参数转换
            List<ProductDto> productDtoList = productListByPageConverter.item2Dtos(itemList);
            //利用listIterator获取切分开的图片信息
            ListIterator<ProductDto> listIterator = productDtoList.listIterator();
            while (listIterator.hasNext()) {
                ProductDto next = listIterator.next();
                String url = next.getPicUrl();
                if (url != null && !"".equals(url)) {
                    String[] strings = url.split(",");
                    next.setPicUrl(strings[0]);
                }
            }
            //封装结果
            allProductResponseResult.setTotal((long) total);
            allProductResponseResult.setData(productDtoList);
            allProductResponse.setAllProductResponseResult(allProductResponseResult);
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }
        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {

        RecommendResponse recommendResponse = new RecommendResponse();

        try {
            //获取热门推荐信息
            Panel panel = panelMapper.selectByPrimaryKey(RECOMMEND_PANEL_ID);
            //参数转换
            PanelDto panelDto = contentConverter.panen2Dto(panel);
            //获取推荐商品详情
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(RECOMMEND_PANEL_ID);
            //参数转换
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            //封装结果
            panelDto.setPanelContentItems(panelContentItemDtos);
            //执行成功
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            Set<PanelDto> panelDtos = new HashSet<>();
            panelDtos.add(panelDto);
            recommendResponse.setPanelContentItemDtos(panelDtos);
        } catch (Exception e) {
            e.printStackTrace();
            recommendResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            recommendResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return recommendResponse;
    }
}
